﻿using System;
using System.Collections.Generic;

namespace MiP.XmlCmd.Shell.Variables
{
    public class StringVariableParser : IStringVariableParser
    {
        private const string FormatMessage = "Variable '{0}' can not be parsed, because it is not in the format 'name=value'.";

        public KeyValuePair<string, string> Parse(string assignment)
        {
            if (!assignment.Contains("="))
                throw new FormatException(string.Format(FormatMessage, assignment));

            int position = assignment.IndexOf('=');

            if (position == 0)
                throw new FormatException(string.Format(FormatMessage, assignment));

            string name = assignment.Substring(0, position);
            string value = assignment.Substring(position + 1, assignment.Length - position - 1);

            return new KeyValuePair<string, string>(name, value);
        }
    }
}